#include <QString>
#include <QtTest>

#include "mocktimerservice.h"
#include "MockViewerInterface.h"

#include "ScriptEngine.h"

class ScriptEngineTest : public QObject
{
   Q_OBJECT

public:
   ScriptEngineTest();

public slots:
   void init();
   void cleanup();

private:
   MockTimerService * m_mockTimerService;
   MockViewerInterface * m_mockViewer;
   ScriptEngine * m_scriptEngine;

private Q_SLOTS:
   void instance();
   void testStartAnimation();
   void testMoreAnimation();
   void testPause();
   void testTimeoutDuration();
   void testSetContent();
   void testGetContent();
};

ScriptEngineTest::ScriptEngineTest()
{
}

void ScriptEngineTest::init()
{
   m_mockTimerService = new MockTimerService();
   m_mockViewer = new MockViewerInterface();
   m_scriptEngine = new ScriptEngine( *m_mockViewer, *m_mockTimerService, this);
}

void ScriptEngineTest::cleanup()
{
   delete m_scriptEngine;
   delete m_mockViewer;
   delete m_mockTimerService;
}

void ScriptEngineTest::instance()
{
   QVERIFY( m_scriptEngine != NULL);
}

void ScriptEngineTest::testStartAnimation()
{
   m_scriptEngine->playScrollAnimation();

   try
   {
      m_mockTimerService->simulateTimeout();
   }
   catch (QString & error)
   {
      QFAIL( error.toLatin1());
   }

   QCOMPARE( m_mockViewer->getActivationRequestNumber(), 1);
}

void ScriptEngineTest::testMoreAnimation()
{
   m_scriptEngine->playScrollAnimation();

   try
   {
      m_mockTimerService->simulateTimeout();
      m_mockTimerService->simulateTimeout();
      m_mockTimerService->simulateTimeout();
   }
   catch (QString & error)
   {
      QFAIL( error.toLatin1());
   }

   QCOMPARE( m_mockViewer->getActivationRequestNumber(), 3);
}

void ScriptEngineTest::testPause()
{
   m_scriptEngine->playScrollAnimation();
   m_scriptEngine->pauseScrollAnimation();

   QCOMPARE( m_mockTimerService->getLastRequestedTime(), -1);
}

/**
 * test that timeout is proportional to block length
 */
void ScriptEngineTest::testTimeoutDuration()
{
   /* calculate timeout for a block with size 10 */
   m_mockViewer->setMockedBlockLength( 10);
   m_scriptEngine->playScrollAnimation();

   int timeoutForLength10 = m_mockTimerService->getLastRequestedTime();

   /* calculate timeout for a block with size 20 */
   m_scriptEngine->pauseScrollAnimation();
   m_mockViewer->setMockedBlockLength( 20);
   m_scriptEngine->playScrollAnimation();

   /* check timeout is proportional */
   int timeoutForLength20 = m_mockTimerService->getLastRequestedTime();

   QVERIFY( timeoutForLength20 == (2 * timeoutForLength10) );
}

void ScriptEngineTest::testSetContent()
{
   QString text = QString("A simple text");
   m_scriptEngine->setScriptContent( text);

   QString readBack;
   m_mockViewer->getScriptContent( readBack);

   QCOMPARE( readBack, text);
}

void ScriptEngineTest::testGetContent()
{
   QString presetText = QString("A simple text");
   m_mockViewer->setScriptContent( presetText);

   QString readBack;
   m_scriptEngine->getScriptContent( readBack);

   QCOMPARE( readBack, presetText);
}

QTEST_APPLESS_MAIN(ScriptEngineTest)

#include "tst_ScriptEngineTest.moc"
